package aiku.numericalcontroloa.Service.getFile;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.stereotype.Service;

import java.nio.file.Path;

import java.nio.file.Paths;

@Service
public class getFileServiceImp implements getFileService {

    @Value("${numericalControlOa.fileBasePath}")
    private String fileBasePath;
    @Value("${numericalControlOa.urlword}")
    String urlword;
    @Value("${numericalControlOa.urlexcel}")
    String urlexcel;
    @Value("${numericalControlOa.urlppt}")
    String urlppt;
    @Value("${numericalControlOa.urlpdf}")
    String urlpdf;
    @Value("${numericalControlOa.urlweizhi}")
    String urlweizhi;

    private Resource getResource(Path path, String fileName) {
        Path file = path.resolve(fileName);
        try {
            Resource resource = new UrlResource(file.toUri());
            if (resource.exists() && resource.isReadable()) {
                return resource;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Resource getWord(String fileName) {
        Path path = Paths.get(fileBasePath + urlword);
        return getResource(path, fileName);
    }

    @Override
    public Resource getExcel(String fileName) {
        Path path = Paths.get(fileBasePath + urlexcel);
        return getResource(path, fileName);
    }

    @Override
    public Resource getPpt(String fileName) {
        Path path = Paths.get(fileBasePath + urlppt);
        return getResource(path, fileName);
    }

    @Override
    public Resource getPdf(String fileName) {
        Path path = Paths.get(fileBasePath + urlpdf);
        return getResource(path, fileName);
    }

    @Override
    public Resource getweizhi(String fileName) {
        Path path = Paths.get(fileBasePath + urlweizhi);
        return getResource(path, fileName);
    }
}
